<?php 
class LoggableListener extends Doctrine_Record_Listener {

    public function preSave(Doctrine_Event $event) {
        $org_obj = $event->getInvoker();
        
        $model_name = get_class($org_obj);
        $model_id = $org_obj->getPrimaryKey();
        
        $user = sfContext::getInstance()->getUser();
        $g_user = $user->getGuardUser();
            
        if(!$org_obj->isNew()){ // edit mode..
            $old_values = $org_obj->getModified(true);
            $new_values = $org_obj->getModified(false);
            
            $last_version_id = ChangeLogsTable::getInstance()->getLastVersionId($model_name, $model_id);
            
            ChangeLogsTable::getInstance()->createNewLog($model_name, $model_id, $old_values, $new_values, $last_version_id);
            
            if($g_user){ // specific logic for this project
                $org_obj['created_by_user_id'] = $old_values['created_by_user_id']; // keep the value of creator the same
                $org_obj['updated_by_user_id'] = $g_user->getId();
                $event->setInvoker($org_obj);
            }
        }else{
            if($g_user){ // specific logic for this project
                $org_obj['created_by_user_id'] = $g_user->getId();
                $event->setInvoker($org_obj);
            }
        }
        
    }

    public function preDelete(Doctrine_Event $event) {
        //TODO: handle deletion
    }

 
}